<!-- <template>
  <error />
</template>

<script>
import error from "../../error/401.vue"
export default {
  name: 'Index',
  components: {
    error
  }
}
</script>

<style scoped></style> -->


<!-- <template>
  <div>
    <el-upload
      class="avatar-uploader"
      :action="uploadPhoto"
      :show-file-list="false"
      accept=".jpg,.png"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template> -->

<!-- <script>
import {uploadPhoto} from '@/api/form'
export default {
  data() {
    return {
      imageUrl: "",
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // 向后端发送文件和其他参数
      const formData = new FormData();
      formData.append("file", file);
      formData.append("username", "admin"); // 根据实际情况传递用户ID或其他标识

      // 发送请求到后端处理上传逻辑
      uploadPhoto(formData)
      .then(response=>{
        this.$message("上传人像成功");
        console.log("图片上传成功",response.data);
      })
      .catch(error=>{
        this.$message.error("上传人像失败");
        console.error("上传人像失败",error);
      })
    },
    beforeAvatarUpload(file) {
      // 在头像上传之前需要做的判断，如判断文件格式
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png"
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG && !isPNG) {
        this.$message.error("只能上传 JPG/PNG 格式的图片");
      }
      
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB");
      }
      this.imageUrl = URL.createObjectURL(file.raw);
      return isJPG && isLt2M;
    },
  },
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style> -->




<template>
  <section v-if="roles == '1'">
    <error />
  </section>
  <section v-else>
    <div class="upload">
      <el-form :inline="true" :rules="formrules">
        <el-form-item style="float: left" label="输入人像姓名:">
          <el-input placeholder="请输入人像姓名......" auto-complete="off" v-model="keyUser"></el-input>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-button :loading="loading" type="success" @click="submitUpload">开始上传</el-button>
        </el-form-item>
      </el-form>
      <el-alert :closable="false" :title="`支持jpg格式，单次只能上传1张图片`" type="info"></el-alert>
      <br />
      <br />
      <el-upload ref="upload" accept="image/jpeg" :action="action" :auto-upload="false" class="upload-content"
        :before-upload="beforeUpload" :close-on-click-modal="false" :data="configForm" :file-list="fileList"
        :headers="headers" :limit="limitNum" list-type="picture-card" :multiple="true" :name="name"
        :on-change="handleChange" :on-exceed="handleExceed" :on-preview="handlePreview" :on-progress="handleProgress"
        :on-remove="handleRemove" :on-success="handleSuccess">
        <i slot="trigger" class="el-icon-plus"></i>
      </el-upload>
      <!-- <div class="dialog-footer" style="position: relative; padding-right: 15px; text-align: right"> -->
      <!-- <el-button type="primary" @click="handleClose">关闭</el-button> -->
      <!-- <el-button :loading="loading" size="small" style="margin-left: 10px" type="success" @click="handleSuccess">开始上传</el-button> -->
      <!-- </div> -->

      <!-- <div>
      <el-dialog append-to-body title="输入人像姓名" :visible.sync="NamedialogVisible">
      <div>
        <el-form ref="nameForm" :model="nameData" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="nameData.name"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="NamedialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSuccess">确定</el-button>
      </div>
    </el-dialog>
    </div> -->

      <el-dialog append-to-body title="查看大图" :visible.sync="dialogVisible">
        <div>
          <img alt="" :src="dialogImageUrl" width="100%" />
        </div>
      </el-dialog>
    </div>
  </section>
</template>

<script>
import { uploadPhoto } from '@/api/form'
import error from "../../error/401.vue"
import { mapGetters } from 'vuex'

export default {
  components: {

    error
  },
  data() {
    return {
      limitNum: 1,
      show: false,
      loading: false,
      dialogVisible: false,
      // NamedialogVisible : false,
      dialogImageUrl: '',
      keyUser: null,
      action: '....',
      headers: {},
      fileList: [],
      picture: 'picture',
      imgNum: 0,
      imgSuccessNum: 0,
      imgErrorNum: 0,
      typeList: null,
      title: '上传',
      dialogFormVisible: false,
      data: {},
      aaa: ''

    }
  },
  computed: {
    ...mapGetters([

      'roles'
    ])
  },
  methods: {
    // handleName(){
    //   this.NamedialogVisible = true;
    // },
    beforeUpload() {
      return new Promise((resolve) => {
        this.$nextTick(() => {
          this.action = '/api/photo/createphoto?username=' + this.keyUser;
          resolve();
        })
      })
    },
    submitUpload() {
      if (this.keyUser == null) {
        this.$message.error("请输入人像姓名！");
        console.error("field", error);
        return;
      }
      this.$refs.upload.submit()
      console.log(this.dialogImageUrl)
    },
    //上传进度
    handleProgress(event, file, fileList) {
      this.loading = true
      this.show = true
    },
    handleChange(file, fileList) {
      if (file.size > 1048576 * this.size) {
        fileList.map((item, index) => {
          if (item === file) {
            fileList.splice(index, 1)
          }
        })
        this.fileList = fileList
      } else {
        this.allImgNum = fileList.length
      }
    },
    handleSuccess(res, file, fileList) {
      // this.NamedialogVisible=false;
      const formData = new FormData();
      formData.append("file", this.fileList[0].raw);
      formData.append("name", "this.keyUser");

      uploadPhoto(formData)
        .then(response => {
          this.$message("上传人像成功");
          console.log("图片上传成功", response.data);
          this.imgNum = this.imgNum + 1
          this.imgSuccessNum = this.imgSuccessNum + 1
          if (fileList.length === this.imgNum) {
            setTimeout(() => {
              this.$baseMessage(
                `上传完成! 共上传${fileList.length}张图片`,
                'success'
              )
            }, 1000)
          }
        })
        .catch(error => {
          this.$message.error("上传人像失败");
          console.error("上传人像失败", error);
        })
      setTimeout(() => {
        this.loading = false
        this.show = false
      }, 1000)
    },
    handleRemove(file, fileList) {
      this.imgNum = this.imgNum - 1
      this.allNum = this.allNum - 1
    },
    handlePreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleShow(data) {
      this.title = '上传'
      this.data = data
      this.dialogFormVisible = true
    },
    handleExceed() {
      this.$message.error("只能上传一张照片！");
      this.$baseMessage(
        `当前限制选择 ${this.limitNum} 个文件`,
        'error'
      )
    },
  }

}
</script>
<style lang="scss" scoped>
.upload {
  height: 500px;

  .upload-content {
    .el-upload__tip {
      display: block;
      height: 30px;
      line-height: 30px;
    }

    ::v-deep {
      .el-upload--picture-card {
        width: 128px;
        height: 128px;
        margin: 3px 8px 8px 8px;
        border: 2px dashed #c0ccda;
      }

      .el-upload-list--picture {
        margin-bottom: 20px;
      }

      .el-upload-list--picture-card {
        .el-upload-list__item {
          width: 128px;
          height: 128px;
          margin: 3px 8px 8px 8px;
        }
      }
    }
  }
}
</style>


